<?php
/**
编辑文章与数据库关联
 */
//获取修改后的文章数据
$articleId = $_POST['article_id'];
$categoryId = $_POST['category_id'];
$articleTitle= $_POST['article_title'];
$articleIntro = $_POST['article_intro'];
$content = $_POST['content'];

//检测文章Id
if (empty($articleId)){
    echo "文章id错误";
    exit();
}

//文章标题5-50个字
if (!(mb_strlen($articleTitle) >= 5 && mb_strlen($articleTitle) <= 50)){
    echo "文章标题5-50个字<br/>";
    echo "<a href='javascript:history.go(-1)'>返回上一页</a>";
    exit();
}
//所属分类Id不能为空
if (empty($categoryId)){
    echo "所属分类Id不能为空<br/>";
    echo "<a href='javascript:history.go(-1)'>返回上一页</a>";
    exit();
}
//文章简介50-500个字
if (!(mb_strlen($articleIntro) >= 50 && mb_strlen($articleIntro) <= 500)){
    echo "文章简介50-500个字<br/>";
    echo "<a href='javascript:history.go(-1)'>返回上一页</a>";
    exit();
}
//文章内容100-8000个字
if (!(mb_strlen($content) >= 100 && mb_strlen($content) <= 8000)){
    echo "文章内容100-8000个字<br/>";
    echo "<a href='javascript:history.go(-1)'>返回上一页</a>";
    exit();
}

//设置时区,获取时间
date_default_timezone_set("PRC");
$updateTime = time();

//连接到数据库
$dsn = "mysql:host=127.0.0.1;dbname=blog";
$db = new PDO($dsn, "root", "123456");

//更改默认的utf8为utf8mb4以防中文乱码
$db->exec("set names utf8mb4");

//php语句mysql修改数据
$sql = "update article 
set article_id = '$articleId' , article_title = '$articleTitle', article_intro = '$articleIntro', update_time = '$updateTime' 
where article_id = '$articleId'";
$result = $db->exec($sql);
if ($result){
    echo "编辑文章成功。<a href='article_list.php'>返回文章列表页</a>>";
    exit();
}else{
    echo "编辑文章失败。错误信息：".$db->errorInfo()[2].",请联系管理员123 ";
}